/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt-sample module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/commons
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxtsample.client.ui;

import com.daikit.daikit4gxt.client.DkBaseMain;
import com.daikit.daikit4gxt.client.action.DkBaseAction;
import com.daikit.daikit4gxt.client.action.DkBaseAsyncCallback;
import com.daikit.daikit4gxt.client.action.base.DkBaseShowScreenAction;
import com.daikit.daikit4gxt.client.ui.DkBaseGui;
import com.daikit.daikit4gxt.client.ui.component.toolbar.DkSpacerToolItem;
import com.daikit.daikit4gxtsample.client.Main;
import com.daikit.daikit4gxtsample.client.screen.testscreen.TestScreen;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.toolbar.SeparatorToolItem;
import com.sencha.gxt.widget.core.client.toolbar.ToolBar;


/**
 * The base GUI
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public class Gui extends DkBaseGui
{

	private TextButton testScreenButton;

	private TextButton refreshAllButton;
	private TextButton testMenuButton;
	private TextButton exceptionServerButton;
	private TextButton exceptionClientButton;
	private TextButton testConnectionTimeout;

	/**
	 * Constructor of base GUI
	 */
	public Gui()
	{
		super();
	}

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// RENDERING
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	/**
	 * Method called from super constructor. Can't use Gui attributes initialized in Gui, in this function.
	 */
	@Override
	protected void updateToolbarBeforeStart(final ToolBar tb)
	{

		// Button for User Screen
		testScreenButton = new TextButton(Main.i18n().gui_menu_test_screen(), new SelectEvent.SelectHandler()
		{
			@Override
			public void onSelect(final SelectEvent event)
			{
				DkBaseShowScreenAction.get(TestScreen.class).execute();
			}
		});
		testScreenButton.setIcon(DkBaseMain.icons().table_16());
		tb.add(testScreenButton);

		// Button for User Screen
		exceptionServerButton = new TextButton(Main.i18n().gui_menu_test_exception_server_side(), new SelectEvent.SelectHandler()
		{
			@Override
			public void onSelect(final SelectEvent event)
			{
				final DkBaseAction<Void> exceptionClientSideAction = new DkBaseAction<Void>(Main.i18n()
						.action_test_exception_loading_label())
				{

					@Override
					protected void run()
					{
						Main.controller().rpcMiscs().testException(new DkBaseAsyncCallback<Void>(this, true)
						{
							@Override
							public void processSuccess(final Void result)
							{
								// Should never happen
							}
						});
					}
				};
				exceptionClientSideAction.execute();
			}
		});
		exceptionServerButton.setIcon(DkBaseMain.icons().refresh_red_16());
		tb.add(exceptionServerButton);

		// Button for User Screen
		exceptionClientButton = new TextButton(Main.i18n().gui_menu_test_exception_client_side(), new SelectEvent.SelectHandler()
		{
			@Override
			public void onSelect(final SelectEvent event)
			{
				final DkBaseAction<Void> exceptionClientSideAction = new DkBaseAction<Void>(Main.i18n()
						.action_test_exception_loading_label())
				{
					@Override
					protected void run()
					{
						throw new RuntimeException("Test exception");
					}
				};
				exceptionClientSideAction.execute();
			}
		});
		exceptionClientButton.setIcon(DkBaseMain.icons().refresh_red_16());
		tb.add(exceptionClientButton);

		testConnectionTimeout = new TextButton(Main.i18n().gui_menu_test_connection_timeout(), new SelectEvent.SelectHandler()
		{
			@Override
			public void onSelect(final SelectEvent event)
			{
				final DkBaseAction<Void> testAction = new DkBaseAction<Void>(Main.i18n().action_test_loading_label())
				{
					@Override
					protected void run()
					{
						Main.controller().rpcMiscs().testConnectionTimeout(new DkBaseAsyncCallback<Void>(this, true)
						{
							@Override
							public void processSuccess(final Void result)
							{
								// Should never happen
							}
						});
					}
				};
				testAction.execute();
			}
		});
		testConnectionTimeout.setIcon(DkBaseMain.icons().refresh_red_16());
		tb.add(testConnectionTimeout);


		// ------------- MENU CONTENT ------------

		if (Main.config().isInTestMode())
		{
			testMenuButton = createTestButton();
			tb.add(new SeparatorToolItem());
			tb.add(testMenuButton);
			tb.add(new SeparatorToolItem());
		}
	}

	@Override
	protected void updateToolbarBeforeDisplayName(final ToolBar tb)
	{

		refreshAllButton = new TextButton(Main.i18n().gui_menu_refresh_all(), new SelectEvent.SelectHandler()
		{
			@Override
			public void onSelect(final SelectEvent event)
			{
				Main.controller().refreshScreen(true);
			}
		});
		refreshAllButton.setIcon(Main.icons().refresh_16());
		tb.add(refreshAllButton);

		tb.add(new DkSpacerToolItem(5));
		tb.add(new SeparatorToolItem());
		tb.add(new DkSpacerToolItem(5));
	}

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// GETTERS - SETTERS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// METHODS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	@Override
	protected String getLoggedAsUsername()
	{
		return Main.model().getUserLogged().getAsSimpleString();
	}

	@Override
	public void invalidateUi()
	{
		super.invalidateUi();
		final boolean isLogged = Main.model().isLogged();
		testScreenButton.setEnabled(isLogged);
		testMenuButton.setEnabled(isLogged);
		refreshAllButton.setEnabled(isLogged);
	}

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// TEST
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	private TextButton createTestButton()
	{
		final TextButton buttonTestMenu = new TextButton("TEST", new SelectHandler()
		{
			@Override
			public void onSelect(final SelectEvent event)
			{
				Main.model().debugModel();
			}
		});
		buttonTestMenu.setIcon(DkBaseMain.icons().config_16());
		return buttonTestMenu;
	}
}
